import sys
import csv

def calculateMean(sum, count):
    return sum / float(count)
    
def calculateVariance(sum, sqr_sum, count):
    return (sqr_sum / (count - 1)) - ((sum*sum) / (count * (count - 1)))

def header(file_name):
    return '''
function drawVisualization_''' + file_name + '''() {  
    var data = new google.visualization.DataTable();
    data.addColumn('number', 'cap');
    data.addColumn('number', 'Red');
    data.addColumn('number', 'Green');
    data.addColumn('number', 'Blue');'''

def footer(file_name):
    return '''
    new google.visualization.LineChart(document.getElementById(\'''' + file_name + '''\')).
    draw(data, {curveType: "function",
                pointSize: 5,
                colors:['Red','Green','Blue'],
                width: 500, height: 400,
                vAxis: {maxValue: 10}}
    );}'''
    
file_name = sys.argv[1]
file = open(file_name)
reader = csv.DictReader(file)

red_sum = 0
red_sqr_sum = 0

green_sum = 0
green_sqr_sum = 0

blue_sum = 0
blue_sqr_sum = 0

count = 0

result = []

index = 0

for row in reader:
    if row['RedCap'] == str(index):
        red = int(row['Red'])
        green = int(row['Green'])
        blue = int(row['Blue'])
    
        red_sum += red
        red_sqr_sum += (red * red)
        
        green_sum += green
        green_sqr_sum += (green * green)
        
        blue_sum += blue
        blue_sqr_sum += (blue * blue)
        
        count += 1
    else:
        result.insert(index, {})
    
        result[index]['Red'] = (calculateMean(red_sum, count), calculateVariance(red_sum, red_sqr_sum, count))
        result[index]['Green'] = (calculateMean(green_sum, count), calculateVariance(green_sum, green_sqr_sum, count))
        result[index]['Blue'] = (calculateMean(blue_sum, count), calculateVariance(blue_sum, blue_sqr_sum, count))
    
        red = int(row['Red'])
        green = int(row['Green'])
        blue = int(row['Blue'])
    
        red_sum = red
        red_sqr_sum = (red * red)
        
        green_sum = green
        green_sqr_sum = (green * green)
        
        blue_sum = blue
        blue_sqr_sum = (blue * blue)
        
        count = 1
        index = int(row['RedCap'])

result.insert(index, {})
    
result[index]['Red'] = (calculateMean(red_sum, count), calculateVariance(red_sum, red_sqr_sum, count))
result[index]['Green'] = (calculateMean(green_sum, count), calculateVariance(green_sum, green_sqr_sum, count))
result[index]['Blue'] = (calculateMean(blue_sum, count), calculateVariance(blue_sum, blue_sqr_sum, count))
        
print header(file_name.replace(".csv",''))
        
for i in xrange(len(result)):
    red_mean = result[i]['Red'][0]
    green_mean = result[i]['Green'][0]
    blue_mean = result[i]['Blue'][0]
    print 'data.addRow([',i,',',
    print red_mean,',',
    print green_mean,',',
    print blue_mean,']);'
    
print footer(file_name.replace(".csv",''))